package com.hanlp.top_interview_150.数组;

/**
 * 55. 跳跃游戏
 */
public class A09 {

    public static void main(String[] args) {

    }

    public static boolean canJump(int[] nums) {
        if(nums.length == 1){
            return true;
        }
        boolean calculateFlag = false;
        int step = 0;

        for (int i = nums.length -2; i >=0 ; i--) {
            if(!calculateFlag){
                if(nums[i] != 0){
                    continue;
                }else {
                    calculateFlag = true;
                    step = 1;
                }
            }else {
                if(nums[i] > step){
                    calculateFlag = false;
                }else {
                    step++;
                }
            }

        }
        return !calculateFlag;
    }


    public boolean canJump2(int[] nums) {

        if(nums.length == 1){
            return true;
        }

        int cover = 0;

        for (int i = 0; i <= cover; i++) {
            cover = Math.max(nums[i] + i,cover);
            if (cover >= nums.length - 1){
                return true;
            }
        }

        return false;

    }
}
